package com.pxmeta.app.controller.community;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Maps;
import com.pxmeta.chat.entity.dto.SetDisturbDTO;
import com.pxmeta.circle.entity.Circle;
import com.pxmeta.circle.entity.UserCircle;
import com.pxmeta.circle.entity.dto.UpdateCircleDTO;
import com.pxmeta.circle.entity.vo.CreateCircleVO;
import com.pxmeta.circle.service.ICircleService;
import com.pxmeta.circle.service.IUserCircleService;
import com.pxmeta.common.constant.HttpStatus;
import com.pxmeta.common.constant.Status;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.enums.CircleUserLevelEnum;
import com.pxmeta.common.utils.AuthUtil;
import com.pxmeta.common.utils.SecurityUtils;
import com.pxmeta.dynamic.entity.dto.ReportCircleDTO;
import com.pxmeta.dynamic.entity.dto.ReportUserDTO;
import com.pxmeta.user.service.IUserReportService;
import com.pxmeta.user.service.IUserService;
import com.pxmeta.verify.service.VerifyService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/circle")
@RequiredArgsConstructor
@Api(tags = "圈子", value = "圈子")
@CrossOrigin
public class CircleController {

    private final ICircleService circleService;
    private final IUserReportService userReportService;
    private final IUserService userService;


    @ApiOperation("获取圈子列表")
    @GetMapping("/getList")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "type", value = "类型: 1学校、2职业、3游戏、4音乐"),
                    @ApiImplicitParam(name = "page", value = "页码"),
                    @ApiImplicitParam(name = "size", value = "每页记录数")
            }
    )
    public AjaxResult<List<Circle>> getCircleList(@RequestParam(required = false) Integer type,
                                    @RequestParam Integer page,
                                    @RequestParam Integer size,
                                    @RequestParam(required = false) String name) {
        Long userId = 0L;
        try{
           userId = SecurityUtils.getUserId();
        }catch (Exception e){
        }

        return AjaxResult.success(circleService.getCircleList(userId,type, page, size,name));
    }

    @ApiOperation("获取推荐圈子列表")
    @GetMapping("/getRecommendCircles")
    public AjaxResult<List<Circle>> getRecommendCircles(){
        Long userId = SecurityUtils.getUserId();
        return AjaxResult.success(circleService.getCircleList(userId,0, 1, 6,null));
    }


    @SaCheckLogin
    @GetMapping("/disturbCircle")
    @ApiOperation("屏蔽圈子")
    public AjaxResult disturbUser(@RequestBody SetDisturbDTO dto){
        Long userId = SecurityUtils.getUserId();
        userService.disturbCircle(userId,dto);
        return AjaxResult.success();
    }

    @SaCheckLogin
    @PostMapping("/reportCircle")
    @ApiOperation("用户举报")
    public AjaxResult reportUser(@RequestBody ReportCircleDTO dto) {
        Long userId = SecurityUtils.getUserId();
        userReportService.report(dto,userId);
        return AjaxResult.success("举报成功！");
    }


    @SaCheckLogin
    @ApiOperation("获取系统圈子列表")
    @GetMapping("/getSysList")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "type", value = "类型: 1学校、2职业、3游戏、4音乐"),
            }
    )
    public AjaxResult getSysCircleList(@RequestParam Integer type){
        List<Circle> list = circleService.getSysCircleList(type);
        return AjaxResult.success(list);

    }


    @SaCheckLogin
    @ApiOperation("创建圈子")
    @PostMapping("/createCircle")
    public AjaxResult createCircle(@RequestBody CreateCircleVO circle) {
        Long userId = SecurityUtils.getUserId();
        Circle ktCircle = new Circle();
        BeanUtils.copyProperties(circle, ktCircle);
        ktCircle.setHot(0L);
        circleService.create(ktCircle, userId);
        return AjaxResult.success();
    }


    @SaCheckLogin
    @ApiOperation("修改圈子")
    @PutMapping("/updateCircle")
    public AjaxResult updateCircle(@RequestBody @Valid UpdateCircleDTO dto) {
        Long id = dto.getId();
        //必须是圈主权限才能修改圈子
        String role = AuthUtil.getRole(id, CircleUserLevelEnum.CREATOR.getLevel());
        StpUtil.checkRole(role);
        Long userId = SecurityUtils.getUserId();
        circleService.updateCircle(dto,userId);
        return AjaxResult.success();
    }



    @SaCheckLogin
    @GetMapping("/shareCircle")
    @ApiOperation("分享圈子")
    public AjaxResult shareDynamic(@RequestParam Long circleId){
        Long userId = SecurityUtils.getUserId();
        Circle circle = circleService.getById(circleId);
        Integer shareCount = circle.getShareCount();
        shareCount ++;
        circle.setShareCount(shareCount);
        circleService.updateById(circle);
        String url = "https://h5.pengxinmeta.com/circle?id="+circleId+"&userId="+userId;
        Map<String, Object> map = Maps.newHashMap();
        map.put("shareUrl",url);
        return AjaxResult.success(map);
    }


}
